$(function () {

    laypage.dir = '/res/lib/laypage/skin/laypage.css ';

    function pageTable(list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {
            str += '<tr data-id="' + list[i].id + '" >' +
                '<td>' + list[i].id + '</td>' +
                '<td>' + list[i].cardNum + '</td>' +
                '<td>' + list[i].title + '</td>' +
                '<td>' + list[i].typeInfo + '</td>' +
                '<td>' + list[i].price + '</td>' +
                '<td>' + list[i].nickName + '</td>' +
                '<td>' + list[i].statusInfo + '</td>' +
                '<td>' + list[i].info + '</td>' +
                '<td>' + new Date(list[i].endTime).format('yyyy/MM/dd') + '</td>' +
                '<td>' +
                (function(){
                    if(list[i].status != 2){
                       return '<a class="btn btn-primary btn-xs btn-modify" href="#/member/ticketedit/' + list[i].id + '" >查看编辑</a> ' +
                        '<a class="btn btn-danger btn-xs btn-delete" >删除</a> ';
                    }
                })() +
                '</td>' +
                '</tr>'
        }

        return str
    }

    var ticketGenerate = function () {
        return '<div style="padding: 10px 0"> ' +
            '<div class="form-group">' +
            '<label class="col-sm-4 control-label">标题:</label>' +
            '<div class="col-sm-7">' +
            '<input name="genTitle" type="text" class="form-control input-sm"  placeholder="标题" value="通用兑换券"/>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-4 control-label">优惠券兑换金额:</label>' +
            '<div class="col-sm-7">' +
            '<input name="amount" type="number" class="form-control input-sm"  placeholder="兑换金额"/>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-4 control-label">截止时间:</label>' +
            '<div class="col-sm-7">' +
            '<input type="text" class="form-control" name="endTime" id="endTime"/>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-4 control-label">类型:</label>' +
            '<div class="col-sm-7">' +
            '<select class="form-control" name="type" ><option value="0">优惠券</option></select>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-4 control-label">数量:</label>' +
            '<div class="col-sm-7">' +
            '<input name="num" type="number" class="form-control input-sm"  placeholder="数量"/>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-4 control-label">说明:</label>' +
            '<div class="col-sm-7">' +
            '<input name="info" type="text" class="form-control input-sm"  placeholder="说明"/>' +
            '</div>' +
            '</div>' +
            '</div>'
    }

    var memberPage = new PageCreate({
        url: '/ticket/doPageQuery',
        container: $("#table tbody"),
        pagination: $("#page"),
        success: function (container, data, body) {
            container.html(pageTable(data.list));

            //批量新增优惠券
            $('#ticket-generate').on('click', function () {
                $.rootOpenDialog({
                    title: "优惠券生成",
                    btn: ["生成"],
                    content: ticketGenerate(),
                    success:function(){
                        laydate.render({
                            elem: '#endTime'
                            ,type: 'datetime'
                            , format:'yyyy/MM/dd'
                            , value: '2099/12/12'
                        });
                    },
                    yes: function (index, lay) {
                        var yesLoad = layer.load(0);
                        $.restPost('/ticket/doGenerate', {
                            token: window.localStorage.getItem('token'),
                            num: $('.form-control[name="num"]').val(),
                            price: $('.form-control[name="amount"]').val(),
                            title: $('.form-control[name="genTitle"]').val(),
                            info: $('.form-control[name="info"]').val(),
                            type: $('.form-control[name="type"]').val(),
                            endTime:$('.form-control[name="endTime"]').val()
                        }).done(function (msg, data) {
                            layer.close(yesLoad)
                            $.rootSuccessDialog(msg, 2000, function () {
                                layer.close(index)
                                memberPage.init(body)
                            })
                        }).fail(function (msg) {
                            layer.close(yesLoad)
                            $.rootErrorDialog(msg, 2000)
                        })
                    }
                });
            });

            //删除优惠券
            $('.btn-delete').off('click').on('click',function(){
                var id = $(this).parents('tr').data('id');
                layer.confirm("确定进行此操作吗？", function (index) {
                    $.restPost('/ticket/doRemove/' + id,{}).done(function(mess,data){
                        $.rootSuccessDialog(mess,1500);
                        layer.close(index)
                        init();
                    }).fail(function(mess){
                        $.rootErrorDialog(mess,1500);
                    })
                });

            });
        }
    });


    var searchPage = function () {
        var $formQuery = $(".form-query");

        function queryDatum() {
            memberPage.init({
                pageNum: 1,
                pageSize: 10,
                nickName: $formQuery.find('.form-control[name=nickName]').val() || "",
                cardNum: $formQuery.find('.form-control[name=cardNum]').val() || "",
                title: $formQuery.find('.form-control[name=title]').val() || "",
                price: $formQuery.find('.form-control[name=price]').val() || ""
            });
        }

        function resetDatum() {
            $formQuery.find('.form-control[name=nickName]').val("");
            $formQuery.find('.form-control[name=cardNum]').val("");
            $formQuery.find('.form-control[name=price]').val("");
            $formQuery.find('.form-control[name=title]').val("");
            memberPage.init({
                pageNum: 1,
                pageSize: 10
            })
        }

        $("#queryDatum").off("click").on("click", function () {
            queryDatum()
        });

        $("#resetDatum").off("click").on("click", function () {
            resetDatum()
        });
    }

    var init = function () {

        memberPage.init({
            pageNum: 1,
            pageSize: 10
        });

        searchPage();
    };
    init()
});
